define([
    "jQuery",
    "core"
], function (jQuery, core) {

    var userIndex = {};

    userIndex.init = function () {
        $(".dropdown-action").on("click", function (e) {
            $.ajax({
                url: "/DVConsole/user/userFormForIndexEdit?userId=" + $(this).attr("id"),
                type: "get",
                success: function (html) {
                    $('.dropdown-user-menu').html(html);
                    stopPropagation();
                    addSaveAction();
                    addCloseAction();
                    addUserInfoAction();
                }
            })
        });

        function stopPropagation() {
            $("#userEdit").find("*").each(function () {
                $(this).click(function (e) {
                    e.stopPropagation();
                })
            });
        }

        function addCloseAction() {
            $(".btn-close").on("click", function (e) {
                $(".dropdown-action").parent("li").removeClass("open");
            })
        }

        function addSaveAction() {
            $(".btn-save").on("click", function (e) {
                core.validator($("#recordForm"), function () {
                    $.ajax({
                        url: "/DVConsole/user/rest/save",
                        type: "post",
                        data: $("#recordForm").serialize(),
                        dataType: "json",
                        success: function (msg) {
                            if (core.message(msg)) {
                                core.loadContent("/");
                            }
                            $(".dropdown-action").parent("li").removeClass("open");
                            if (msg.code == 200) {
                                $(".user-name-label").html('<span class="fa fa-user" style="margin-right: 8px"></span>'
                                    + $("#user-name").val());
                            }
                            callback && callback(msg);
                        }
                    });
                });
            });
        }

        function addUserInfoAction() {
            $(".user-info-action").on("click", function (e) {
                var value = $(this).is(':checked');
                if (value == true) {
                    $('.user-info').show();
                } else {
                    $(".user-info").hide();
                }
            })
        }

        function DropDown(el) {
            this.dd = el;
            this.placeholder = this.dd.children('span');
            this.opts = this.dd.find('ul.dropdown > li');
            this.val = '';
            this.index = -1;
            this.initEvents();
        }

        DropDown.prototype = {
            initEvents: function () {
                var obj = this;

                obj.dd.on('click', function (event) {
                    $(this).toggleClass('active');
                    return false;
                });

                obj.opts.on('click', function () {
                    //var opt = $(this);
                    location.href = "main?target=cluster&cluster=" + $(this).attr("clusterId");
                    //obj.val = opt.text();
                    //obj.index = opt.index();
                    //obj.placeholder.text('集群: ' + obj.val);
                });
            },
            getValue: function () {
                return this.val;
            },
            getIndex: function () {
                return this.index;
            }
        }

        var dd = new DropDown($('#dd'));

        $(document).click(function () {
            $('.wrapper-dropdown-1').removeClass('active');
        });
    }
    return userIndex;
})